package org.wx.io.nio;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * NIO Client Socket
 *
 * @author wangxun
 * @version 1.0
 * @description: TODO
 * @date 2021/2/1 5:14 下午
 */
@Slf4j
public class NIOClientSocket {


    public static void main(String[] args) throws IOException {
        SocketChannel clientChannel = SocketChannel.open();
        clientChannel.configureBlocking(false);

        //连接服务器
        if (!clientChannel.connect(new InetSocketAddress("127.0.0.1",9090))) {

            while (!clientChannel.finishConnect()) {
                log.info("因为连接需要时间，客户端不会阻塞，可以做其它工作..");
            }

        }

        ByteBuffer wrap = ByteBuffer.wrap("I am wx".getBytes());
        clientChannel.write(wrap);
        System.in.read();

    }
}
